package com.snda.doodlepro.app;

import java.lang.Thread.UncaughtExceptionHandler;

import com.snda.doodlepro.data.DoodleDB;
import com.snda.doodlepro.log.LogManager;

import android.app.Application;
import android.util.Log;

public class DoodleApplication  extends Application {
	
	private static final String TAG = "DoodleApplication";
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		LogManager.initialise();
		try {
			new DoodleDB(getApplicationContext());
		} catch (Exception e) {
			Log.d(TAG, "DoodleDB(Context context) exception:" + e.getMessage());
		}
		
		LogManager.log(TAG, "onCreate", "system", "DoodleApp start");
		
		Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
			public void uncaughtException(Thread thread, Throwable ex) {
				Log.e(TAG, "uncaughtException.@thead:" + thread.getId() + "("
						+ thread.getName() + "):" + ex.getMessage());

				ex.printStackTrace();

				LogManager.log(TAG, "Application", "UncaughtException", ex.getMessage());

				System.exit(-1);
			}
		});
		
	}

	@Override
	public void onTerminate() {
		
		DoodleDB.instance().close();
			
		LogManager.log("DoodleApplication", "onTerminate", "system", "DoodleApp terminte");
		LogManager.terminate();
		super.onTerminate();
	}
}
